package com.winai.launcher.utils;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

/**
 * 加密工具类
 * author：wiki on 2018/12/12
 * email：zhengweiqunemail@qq.com
 */
public class MD5Utils {

    public static final String TAG = MD5Utils.class.getSimpleName();

    public static String getFileMD5(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return bytesToHexString(digest.digest());
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    /**
       * 获取文件夹中文件的MD5值
       * 
       * @param file
       * @param listChild
       *            ;true递归子目录中的文件
       * @return
       */

    public static Map<String, String> getDirMD5(File file, boolean listChild) {
        if (!file.isDirectory()) {
            return null;
        }
        Map<String, String> map = new HashMap<String, String>();
        String md5;
        File files[] = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (f.isDirectory() && listChild) {
                map.putAll(getDirMD5(f, listChild));
            } else {
                md5 = getFileMD5(f);
                if (md5 != null) {
                    map.put(f.getPath(), md5);
                }
            }
        }
        return map;
    }

    public static String getMD5Checksum(String filename) throws Exception {
        byte[] b = createChecksum(filename, "MD5");
        return byte2String(b);
    }

    /**
     * 校验
     *
     * @param filename 文件名
     * @param strType  校验类型--SHA-1,SHA-256,SHA-384,SHA-512,MD5,MD2
     * @return byte[]
     */
    private static byte[] createChecksum(String filename, String strType) throws Exception {
        InputStream fis = new FileInputStream(filename);          //将流类型字符串转换为String类型字符串

        byte[] buffer = new byte[1024];
        MessageDigest complete = MessageDigest.getInstance(strType);
        int numRead;

        do {
            numRead = fis.read(buffer);    //从文件读到buffer，最多装满buffer
            if (numRead > 0) {
                complete.update(buffer, 0, numRead);  //用读到的字节进行strType的计算，第二个参数是偏移量
            }
        } while (numRead != -1);

        fis.close();
        return complete.digest();
    }

    private static String byte2String(byte[] bytes) {
        StringBuilder result = new StringBuilder();

        for (byte b : bytes) {
            //加0x100是因为有的b[i]的十六进制只有1位
            result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        }
        Log.i(TAG, "checkSum: " + result.toString());
        return result.toString();
    }
}


